package ahblue.cn.tanzi.ui.home.special;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.adapter.BRAdapter;
import ahblue.cn.tanzi.adapter.RvHolder;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.home.SpecalModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeSpecalActivity extends BaseActivity {

    @BindView(R.id.recy)
    RecyclerView recyview;
    @BindView(R.id.refresh)
    SwipeRefreshLayout swipeRefreshLayout;
    private int page=0;

    private List<SpecalModel> dataList = new ArrayList<>();
    private BRAdapter<SpecalModel> adapter;
    //handler
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    //swipeRefreshLayout.setEnabled(false);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_spec);
        ButterKnife.bind(this);
        initView();
        initData();
        initLoadMoreListener();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getSpecalMsg(page, 10, mSpecalListcallback);
    }

    private void initView() {
        recyview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BRAdapter<SpecalModel>(this, R.layout.activity_specal_item, dataList) {
            @Override
            protected void convert(RvHolder holder, SpecalModel specalModel, int position) {
                ImageLoadUtil.load(HomeSpecalActivity.this, specalModel.image, (ImageView) holder.getView(R.id.img));
                holder.setText(R.id.name,specalModel.title);
                holder.setText(R.id.sign,specalModel.sign_people+"人订阅");
                if (specalModel.label.size()>0){
                    holder.setText(R.id.lable,specalModel.label.get(0));
                    holder.setVisible(R.id.lable,true);
                }else {
                    holder.setText(R.id.lable,"");
                    holder.setVisible(R.id.lable,false);
                }

                holder.setText(R.id.price,getResources().getString(R.string.renminbi)+specalModel.special_price);
            }
        };
        recyview.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//        swipeRefreshLayout.setProgressBackgroundColor(R.color.colorAccent);
        //swipeRefreshLayout.setPadding(20, 20, 20, 20);
        //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);
        //swipeRefreshLayout.setDistanceToTriggerSync(50);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dataList.clear();
                        page=0;
                        initData();
                        mHandler.sendEmptyMessage(1);
                    }
                }).start();
            }
        });

    }

    private void initLoadMoreListener() {

        //对Recyclerview 的一个滑动监听
        recyview.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem ;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==adapter.getItemCount()){
                    page++;
                    initData();
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem=layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    ApiCallback<List<SpecalModel>> mSpecalListcallback = new ApiCallback<List<SpecalModel>>() {
        @Override
        public void onSuccess(List<SpecalModel> data) {
            dataList.addAll(data);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    @OnClick({R.id.btn_back, R.id.imageButtonSearch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.imageButtonSearch:
                break;
        }
    }
}
